﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;


public partial class TruckManage_TruckList : BasePage
{
    IBNTruckHandler truckManager = new BNTruckHandler();
    DataBase db = new DataBase();

    private static bool isUpdate = false;
    private static bool isDel = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1038"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
                else
                {
                    //判断用户有没有添加的权限
                    if (strPermission.Contains("1039"))
                    {
                        lblTruckAdd.Visible = true;
                    }
                    else
                    {
                        lblTruckAdd.Visible = false;
                    }
                    //判断用户有没有修改的权限
                    if (strPermission.Contains("1040"))
                    {
                        isUpdate = true;
                    }
                    else
                    {
                        isUpdate = false;
                    }
                    //判断用户有没有删除的权限
                    if (strPermission.Contains("1041"))
                    {
                        isDel = true;
                    }
                    else
                    {
                        isDel = false;
                    }
                }

                //CheckPagePermission();//判断当前用户有没有添加删除更新的权限

            }
           
            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "asc";
            dataBind();
        }
    }
   

    private void dataBind()
    {
        string clientId = Session["LoginUserClientID"].ToString();
        string sqlStr = "SELECT * FROM BN_Truck where ClientID=" + (clientId == null || clientId.Trim() == "" ? "0" : clientId);
        DataTable dt = truckManager.GetList(sqlStr, null);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvTruckList.DataSource = view;
        gvTruckList.DataBind();
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTotal.Text = dt.Rows.Count.ToString();
        }
        else
        {
            lblTotal.Text = "0";
        }
    }
   

    protected void gvTruckList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTruckList.PageIndex = e.NewPageIndex;
        dataBind();

    }
    protected void gvTruckList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!truckManager.DeleteByID(int.Parse(e.CommandArgument.ToString())))
            {
                Jscript.AlertInAjax(Resources.Messages.DeleteFailed, UpdatePanel1);
            }
        }
        dataBind();

    }
    protected void gvTruckList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvTruckList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void gvTruckList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnDel = (LinkButton)e.Row.FindControl("lbtnDel");
            Label lblEdit = (Label)e.Row.FindControl("lblUpdate");

            if (isUpdate)
            {
                lblEdit.Visible = true;
            }
            else
            {
                lblEdit.Visible = false;
            }

            if (isDel)
            {
                btnDel.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
            }
        }
    }

    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvTruckList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvTruckList.PageCount)
            {
                this.gvTruckList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
        }
    }


    public string GetDriverName(object id)
    {
        string name = string.Empty;
        IBNDriverHandler driverManager = new BNDriverHandler();
        BN_DriverQuery query = driverManager.GetDriverByID(int.Parse(id.ToString()));
        if (query != null)
        {
            name = query.Name;
        }
        return name;
    }

    public string GetTypeName(object id)
    {
        string name = string.Empty;
        ISyContainerModelHandler cmm = new SyContainerModelHandler();
        Sy_ContainerModelQuery query = cmm.GetModel(int.Parse(id.ToString()));
        if (query != null)
        {
            name = query.Model;
        }

        return name;
    }
}
